﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity.Configuration;
using System.Windows.Forms;
using AMStudio.DelegateDispather;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.Diagnostics;

namespace AMStudio.Common
{
    public class WinFormBootstrapper
    {
        /// <summary>
        /// Runs the bootstrapper process.
        /// </summary>
        public void Run()
        {
            this.Run(true);
        }
        public virtual void Run(bool runWithDefaultConfiguration)
        {
            //LogEntry entry = new LogEntry();
            //entry.Message = "TEST";
            //entry.Severity = System.Diagnostics.TraceEventType.Error;
            //Logger.Write(entry);
            this.Container = this.CreateContainer();
            this.ConfigureContainer();
            this.ConfigureServiceLocator();
            //Application.Run(Container.Resolve<AWinFormShell>("BaseUI"));  
            RuntimeDB.Init("MachineDescription");
            UserInterfaceDB.Init();
            UserDB.Init();
            WorkorderDB.Init();
            LanguageDB.Init();
            TranslationDB.Init();
            IUserInterface login=Container.Resolve<IUserInterface>("LoginForm");
            if (login.ActivateUI())
            {
                ApplicationContext context = new ApplicationContext(Container.Resolve<AWinFormShell>("BaseUI"));
                context.ThreadExit += new EventHandler(context_ThreadExit);
                //AEventManager myManager = ServiceLocator.Current.GetInstance<IEventManager>("EventManager") as AEventManager;
                ////int a = myManager.GetHashCode();
                //myManager.RunAsync();
                Application.Run(context);

            }
        }

        void context_ThreadExit(object sender, EventArgs e)
        {
            AEventManager myManager = ServiceLocator.Current.GetInstance<IEventManager>("EventManager") as AEventManager;
            //int a = myManager.GetHashCode();
            myManager.Stop();

            Process.GetCurrentProcess().Kill();
        }

        /// <summary>
        /// Gets the default <see cref="IUnityContainer"/> for the application.
        /// </summary>
        /// <value>The default <see cref="IUnityContainer"/> instance.</value>  
        public IUnityContainer Container { get; protected set; }
        /// <summary>
        /// Creates the <see cref="IUnityContainer"/> that will be used as the default container.
        /// </summary>
        /// <returns>A new instance of <see cref="IUnityContainer"/>.</returns>    
        protected virtual IUnityContainer CreateContainer()
        {
            return new UnityContainer();
        }
        /// <summary>
        /// Configures the <see cref="IUnityContainer"/>. May be overwritten in a derived class to add specific
        /// type mappings required by the application.
        /// </summary>
        protected virtual void ConfigureContainer()
        {
            Container.LoadConfiguration();
            Container.RegisterType(typeof(IServiceLocator), typeof(UnityServiceLocatorAdapter), new ContainerControlledLifetimeManager());
            //Container.RegisterInstance(typeof(ExceptionManager),
            //    EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>(), 
            //    new ContainerControlledLifetimeManager());

        }

        /// <summary>
        /// Configures the LocatorProvider for the <see cref="ServiceLocator" />.
        /// </summary>
        protected virtual void ConfigureServiceLocator()
        {
            ServiceLocator.SetLocatorProvider(() => this.Container.Resolve<IServiceLocator>());
        }
    }
}
